package engine.ui.equipStrenGthen 
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.ui.NewItemChangeUI;
	
	import newx.BaseObject;
	/**
	 * ...
	 * @author ...WangHaiBo 
	 * ...........www.woniu.com
	 * 装备检索功能
	 */
	public class EquipLeachLogic 
	{
		private static var _self:EquipLeachLogic;
		private var _model:ModelLocator;
		private var _bindManage:BindManage;
		/**
		 * 绑定多个视图
		 */
		//打空
		private var _bindAddHold:uint;
		private var holdItemObj:Object
		//镶嵌
		private var _bindEnchase:uint;
		private var enchaseItemObj:Object;
		//合成
		private var _bindCompose:uint;
		private var composeItemObj:Object;
		//拆解
		private var _bindUnenchase:uint;
		private var unenchaseItemObj:Object
		//升级
		private var _bindEquipupgrade:uint;
		private var equipupgradeItemObj:Object
		//保值
		private var _bindKeepWotrh:uint;
		private var keepWotrhItemObj:Object;
		//置换
		private var _bindRandomstone:uint
		private var randomstoeItemObj:Object;
		//人物
		private var _bindRole:uint;
		//背包
		private var _bindPack:uint;
		//人物视图对象
		private var roleItemObj:Object;
		//背包视图对象
		private var packItemObj:Object;
		
		private var _packEquipArray:Array;
		private var _roleEquipArray:Array;
		private var _packRockArray:Array;
		private var _checkArray	:Array;
		private var _backArray:Array;
		private var _checkItemObj:Object;
		private var _changeType:String;
		private var _lookType:String;
		private var _backFucntion:Function;
		private var isHave:Boolean;
		public static var ROLE_EQUIP:String	=	"ROLE_EQUIP";
		public static var PACK_EQUIP:String	=	"PACK_EQUIP";
		public static var PACK_ROCK:String	=	"PACK_ROCK";
		
		public function EquipLeachLogic() 
		{
			init();
		}
		public static function getInstance():EquipLeachLogic
		{
			if (_self == null) 
			{
				_self	=	new EquipLeachLogic();
			}
			return _self;
		}
		private function init():void
		{
			roleItemObj			=	{ };
			packItemObj			=	{ };
			holdItemObj			=	{ };
			enchaseItemObj		=	{ };
			
			composeItemObj		=	{ };
			unenchaseItemObj	=	{ };
			equipupgradeItemObj	=	{ };
			keepWotrhItemObj	=	{ };
			randomstoeItemObj	=	{ };
			
			
			_packEquipArray	=	[];
			_roleEquipArray	=	[];
			_packRockArray	=	[];
			_model			=	ModelLocator.getInstance();
			_bindManage		=	BindManage.getInstance();
			addBind();
		}
		/**
		 * 注册所要显示的类型
		 * @param	ChangeType_		强化类型
		 * @param	lookType_		查看类型
		 * @param	fun_			回调函数
		 */
		public function register(changeType_:String,lookType_:String,fun_:Function):void
		{
			_changeType		=	changeType_;
			_lookType		=	lookType_;
			_backFucntion	=	fun_;
			callBackFunction();
		}
		/**
		 * 添加邦定
		 */
		private function addBind():void
		{
			_bindRole			=	_bindManage.BindView(_model.scene, View.VIEWPORT_EQUIP, 		onAddRoleEquip, 		onRemoveRoleEquip);
			_bindPack			=	_bindManage.BindView(_model.scene, View.VIEWPORT_TOOL, 			OnAddPackEquip, 		OnRemovePackEquip);
			
			_bindAddHold		=	_bindManage.BindView(_model.scene, View.VIEWPORT_ADDHOLE, 		OnAddHoldEquip, 		OnRemoveHoldEquip);
			_bindEnchase		=	_bindManage.BindView(_model.scene, View.VIEWPORT_ENCHASE, 		OnAddEnchaseEquip, 		OnRemoveEnchaseEquip);
			_bindCompose		=	_bindManage.BindView(_model.scene, View.VIEWPORT_COMPOSE, 		OnAddComposeEquip, 		OnRemoveComposeEquip);
			_bindUnenchase		=	_bindManage.BindView(_model.scene, View.VIEWPORT_UNENCHASE, 	OnAddUnenchaseEquip, 	OnRemoveUnenchaseEquip);
			_bindEquipupgrade	=	_bindManage.BindView(_model.scene, View.VIEWPORT_EQUIPUPGRADE, 	OnAddEquipupgradeEquip, OnRemoveEquipupgradeEquip);
			_bindKeepWotrh		=	_bindManage.BindView(_model.scene, View.VIEWPORT_KEEPWORTH, 	OnAddKeepWotrhEquip, 	OnRemoveKeepWotrhEquip);
			_bindRandomstone	=	_bindManage.BindView(_model.scene, View.VIEWPORT_RANDOMSTONE, 	OnAddRandomstoneEquip, 	OnRemoveRandomstoneEquip);
			
		}
		/**
		 * 删除邦定
		 */
		private function removeBind():void
		{
			_bindManage.UnbindView(View.VIEWPORT_EQUIP, _bindRole);
			_bindManage.UnbindView(View.VIEWPORT_TOOL, _bindPack);
		}
		/**添加打孔中的装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnAddHoldEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			holdItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**移除打孔中的装备;
		 * 
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnRemoveHoldEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			holdItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**添加镶嵌中的装备
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnAddEnchaseEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			enchaseItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**移除镶嵌中的装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnRemoveEnchaseEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			enchaseItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**添加合成中的装备
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnAddComposeEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			composeItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**移除合成中的装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnRemoveComposeEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			composeItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**添加拆解中的装备
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnAddUnenchaseEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			unenchaseItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**移除拆解中的装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnRemoveUnenchaseEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			unenchaseItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**添加强化中的装备
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnAddEquipupgradeEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			equipupgradeItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**移除强化中的装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnRemoveEquipupgradeEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			equipupgradeItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**添加保值中的装备
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnAddKeepWotrhEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			keepWotrhItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**移除保值中的装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnRemoveKeepWotrhEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			keepWotrhItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**添加置换中的装备
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnAddRandomstoneEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			randomstoeItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**移除置换中的装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnRemoveRandomstoneEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			randomstoeItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**添加人物身上装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function onAddRoleEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			Obj.HandleProperties("ViewInt", View.VIEWPORT_EQUIP);
			Obj.HandleProperties("isEnable", true);
			Obj.HandleProperties("equipIndex", equipIndex);
			roleItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**移除人物身上装备;
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function onRemoveRoleEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int	=	Obj["ObjKey"];
			roleItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**
		 * 检测人物身上装备
		 */
		private function againCheckRoleEquipItem():void
		{
			_roleEquipArray	=	[];
			var checkBaseObj:BaseObject
			for (var name:String in roleItemObj) 
			{
					checkBaseObj	=	roleItemObj[name] as BaseObject;
				if (checkBaseObj != null) 
				{
					_roleEquipArray.push(checkBaseObj);
				}
			}
		}
		/**
		 * 添加背包中的装备
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnAddPackEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			Obj.HandleProperties("ViewInt", View.VIEWPORT_TOOL);
			Obj.HandleProperties("isEnable", true);
			var equipIndex:int				=	Obj["ObjKey"];
			packItemObj[String(equipIndex)]	=	Obj;
			itemChangeCheckEquip();
		}
		/**
		 * 移除背包中的装备
		 * @param	ObjID
		 * @param	Obj
		 * @param	data
		 */
		private function OnRemovePackEquip(objID:String,Obj:BaseObject,data:*):void
		{
			var equipIndex:int				=	Obj["ObjKey"];
			packItemObj[String(equipIndex)]	=	null;
			itemChangeCheckEquip();
		}
		/**
		 * 当前所要检测的数组类型
		 */
		private function nowCheckArrayType():void
		{
			switch(_lookType)
			{
				case ROLE_EQUIP:
					_checkArray	=	_roleEquipArray;
					break;
				case PACK_EQUIP:
					_checkArray	=	_packEquipArray;
					break;
				case PACK_ROCK:
					_checkArray	=	_packRockArray;
					break;
			}
		}
		/**
		 * 重新检索装备物品
		 */
		private function againCheckEquipItem():void
		{
			var checkBaseObj:BaseObject;
			//检索装备和宝石
			_packRockArray	=	[];
			_packEquipArray	=	[];
			for (var name:String in packItemObj) 
			{
				checkBaseObj	=	packItemObj[name] as BaseObject;
				if (checkBaseObj != null) 
				{
					if (checkBaseObj.CheckProperties("ItemType")) 
					{
						var itemType:String			=	checkBaseObj.GetProperties("ItemType");
						var subItemType:String		=	checkBaseObj.GetProperties("SubItemType");
						var intensityType:String	=	"0"
						if (checkBaseObj.CheckProperties("IntensityType")) 
						{
							intensityType	=	checkBaseObj.GetProperties("IntensityType");
						}
						if (itemType	==	"105" || itemType	==	"107"	|| intensityType != "0") 
						{
							_packEquipArray.push(checkBaseObj);
							
						}
						else if (subItemType	==	"20004")
						{
							_packRockArray.push(checkBaseObj);
						}
					}
				}
			}
		}
		/**
		 * 当强化中有变化时要重新检索下装备
		 */
		private function itemChangeCheckEquip():void
		{
			var checkBaseObj:BaseObject;
			//人物
			_roleEquipArray	=	[];
			for (var name:String in roleItemObj) 
			{
				checkBaseObj	=	roleItemObj[name] as BaseObject;
				if (checkBaseObj != null) 
				{
					checkBaseObj.HandleProperties("isEnable", true);
					_roleEquipArray.push(checkBaseObj);
				}
			}
			//背包
			_packRockArray	=	[];
			_packEquipArray	=	[];
			for (var namePack:String in packItemObj) 
			{
				checkBaseObj	=	packItemObj[namePack] as BaseObject;
				if (checkBaseObj != null) 
				{
					checkBaseObj.HandleProperties("isEnable", true);
					if (checkBaseObj.CheckProperties("ItemType")) 
					{
						var itemType:String			=	checkBaseObj.GetProperties("ItemType");
						var subItemType:String		=	checkBaseObj.GetProperties("SubItemType");
						var intensityType:String	=	"0"
						if (checkBaseObj.CheckProperties("IntensityType")) 
						{
							intensityType	=	checkBaseObj.GetProperties("IntensityType");
						}
						if (itemType	==	"105" || itemType	==	"107"	|| intensityType != "0") 
						{
								_packEquipArray.push(checkBaseObj);
							
						}
						else if (subItemType	==	"20004")
						{
							_packRockArray.push(checkBaseObj);
						}
					}
				}
			}
			//当前视图对像
			for (var nameObj:String in _checkItemObj) 
			{
				checkBaseObj	=	_checkItemObj[nameObj] as BaseObject;
				if (checkBaseObj != null) 
				{
					checkBaseObj.HandleProperties("isEnable", false);
					if (!checkBaseObj.CheckProperties("ViewInt")) 
					{
						checkBaseObj.HandleProperties("ViewInt", View.VIEWPORT_TOOL);
					}
					if (checkBaseObj.GetProperties("ViewInt") == View.VIEWPORT_EQUIP) 
					{
						isHave	=	false;
						if (checkBaseObj.CheckProperties("equipIndex")) 
						{
							for (var i:int = 0; i < _roleEquipArray.length; i++) 
							{
								if (checkBaseObj.GetProperties("equipIndex") == _roleEquipArray[i]["ObjKey"]) 
								{
									isHave	=	true
								}
							}
						}
						if (isHave) 
						{
							_packEquipArray.push(checkBaseObj);
						}
						else
						{
							_roleEquipArray.push(checkBaseObj);
						}
					}
					else if(checkBaseObj.GetProperties("ViewInt") == View.VIEWPORT_TOOL)
					{
						if (checkBaseObj.CheckProperties("ItemType")) 
						{
							var itemObjType:String			=	checkBaseObj.GetProperties("ItemType");
							var subItemObjType:String		=	checkBaseObj.GetProperties("SubItemType");
							var intensityObjType:String		=	"0";
							if (checkBaseObj.CheckProperties("IntensityType")) 
							{
								intensityType	=	checkBaseObj.GetProperties("IntensityType");
							}
							if (itemObjType	==	"105" || itemObjType	==	"107" || intensityType != "0") 
							{
								_packEquipArray.push(checkBaseObj);
								
							}
							else if (subItemObjType	==	"20004")
							{
								_packRockArray.push(checkBaseObj);
							}
						}
					}
				}
			}
			callBackFunction();
		}
		/**
		 * 检索放入强化视图中的物品
		 */
		private function checkItemObj():void
		{
			var checkBaseObj:BaseObject
			for (var name:String in _checkItemObj) 
			{
				checkBaseObj	=	_checkItemObj[name] as BaseObject;
				if (checkBaseObj != null) 
				{
					checkBaseObj.HandleProperties("isEnable", false);
					if (checkBaseObj.GetProperties("ViewInt") == View.VIEWPORT_EQUIP) 
					{
						_roleEquipArray.push(checkBaseObj);
					}
					else if(checkBaseObj.GetProperties("ViewInt") == View.VIEWPORT_TOOL)
					{
						if (checkBaseObj.CheckProperties("ItemType")) 
						{
							var itemType:String			=	checkBaseObj.GetProperties("ItemType");
							var subItemType:String		=	checkBaseObj.GetProperties("SubItemType");
							var intensityType:String	=	"0"
							if (checkBaseObj.CheckProperties("IntensityType")) 
							{
								intensityType	=	checkBaseObj.GetProperties("IntensityType");
							}
							if (itemType	==	"105" || itemType	==	"107"	|| intensityType != "0") 
							{
								_packEquipArray.push(checkBaseObj);
								
							}
							else if (subItemType	==	"20004")
							{
								_packRockArray.push(checkBaseObj);
							}
						}
					}
				}
			}
		}
		/**
		 * 返回函数
		 */
		private function callBackFunction():void
		{
			_backArray	=	[];
			nowCheckArrayType();
			if (_backFucntion	== null) return;
			switch(_changeType)
			{
				case NewItemChangeUI.EQUIP_HOLD:
					_backArray		=	checkCanHoldEquip();
					_checkItemObj	=	holdItemObj;
					break
				case NewItemChangeUI.EQUIP_ENCHASE:
					if (_lookType == PACK_ROCK) 
					{
						_backArray	=	checkCanComposeRock();
					}
					else
					{
						_backArray	=	checkCanEnchaseEquip();
					}
					_checkItemObj	=	enchaseItemObj;
					break;
				case NewItemChangeUI.EQUIP_COMPOSE:
					_backArray		=	checkCanComposeRock();
					_checkItemObj	=	composeItemObj;
					break;
				case NewItemChangeUI.EQUIP_UNENCHASE:
					_backArray		=	checkCanUnenchaseEquip();
					_checkItemObj	=	unenchaseItemObj;
					break;
				case NewItemChangeUI.EQUIP_UP:
					_backArray		=	checkCanUpEquip();
					_checkItemObj	=	equipupgradeItemObj;
					break;
				case NewItemChangeUI.EQUIP_PROTECTION:
					_backArray	=	checkCanProtectionEquip();
					_checkItemObj	=	keepWotrhItemObj;
					break;
				case NewItemChangeUI.ROCK_CHANGE:
					_backArray		=	checkCanComposeRock();
					_checkItemObj	=	randomstoeItemObj;
					break;
			}
			_backFucntion.call(this,_backArray);
		}
		/**
		 * 检索可打空的装备
		 * @return
		 */
		private function checkCanHoldEquip():Array
		{
			var returnArray:Array	=	[];
			for each (var item:BaseObject in _checkArray) 
			{
				if (item.CheckProperties("ItemType")) 
				{
					var itemType:String	=	item.GetProperties("ItemType");
					var intensityType:String	=	"0";
					//是否为新的类型物品
					if (item.CheckProperties("IntensityType")) 
					{
						intensityType	=	item.GetProperties("IntensityType");
					}
					if (itemType == "105" || itemType == "107") 
					{
						var holeCount:int	=	item.GetProperties("HoleCount");
						if (holeCount < 4)
						{
							returnArray.push(item);
						}
					}
				}
			}
			return returnArray;
		}
		/**
		 * 检索可镶嵌的装备
		 * @return
		 */
		private function checkCanEnchaseEquip():Array
		{
			var returnArray:Array	=	[];
			for each (var item:BaseObject in _checkArray) 
			{
				var holeCount:int	=	item.GetProperties("HoleCount");
				if (addPropNoNum(item) < holeCount) 
				{
					returnArray.push(item);
				}
			}
			return returnArray;
		}
		/**
		 * 检索可合成的宝石头
		 * @return
		 */
		private function checkCanComposeRock():Array
		{
			return _packRockArray;
		}
		/**
		 * 检索可拆解的装备
		 * @return
		 */
		private function checkCanUnenchaseEquip():Array
		{
			var returnArray:Array	=	[];
			for each (var item:BaseObject in _checkArray) 
			{
				if (addPropNoNum(item) > 0) 
				{
					returnArray.push(item);
				}
			}
			return returnArray;
		}
		/**
		 * 检索可升级的装备
		 * @return
		 */
		private function checkCanUpEquip():Array
		{
			var returnArray:Array	=	[];
			for each (var item:BaseObject in _checkArray) 
			{
				if( item.CheckProperties("IntensityType") && (0 != item.GetProperties("IntensityType")) )
				{
					returnArray.push(item);
				}
			}
			return returnArray;
		}
		/**
		 * 检索可保值的装备
		 * @return
		 */
		private function checkCanProtectionEquip():Array
		{
			var returnArray:Array	=	[];
			for each (var item:BaseObject in _checkArray) 
			{
				var itemType:String	=	item.GetProperties("ItemType");
				var intensityType:String	=	"0";
					//是否为新的类型物品
				if (item.CheckProperties("IntensityType")) 
				{
					intensityType	=	item.GetProperties("IntensityType");
				}
				if (itemType	==	"105" || itemType == "107" ||intensityType != "0") 
				{
					var holeCount:int	=	item.GetProperties("HoleCount");
					if (holeCount > 0 || intensityType	!= "0") 
					{
						returnArray.push(item);
					}
				}
			}
			return returnArray;
		}
		/**
		 * 镶嵌属性数量 
		 * @param val
		 * @return 
		 * 
		 */		
		private function addPropNoNum(val:BaseObject):int
		{
			var propNum:int = 0;
			for(var i:int =6; i < 10; i++)
			{
				var propStr:String	=	val.GetProperties("AddPropNo" + i);
				if(propStr !=null && propStr != "" && propStr != "0")
				{
					propNum++;
				}
			}
			return propNum;
		}
		
	}

}